﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System.IO;

namespace RotovapPlatform.Infrastructure.Common.Data;

public class DesignTimeRecipeDbContextFactory : IDesignTimeDbContextFactory<RecipeDbContext>
{
	public RecipeDbContext CreateDbContext(string[] args)
	{
		var optionsBuilder = new DbContextOptionsBuilder<RecipeDbContext>();
		var solutionDir = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\..\\.."));
		var infrastructureDir = Path.Combine(solutionDir, "src", "RotovapPlatform.Infrastructure");
		var folder = Path.Combine(infrastructureDir, "Data");
		Console.WriteLine($"Looking for database in: {folder}");
		if (!Directory.Exists(folder))
			Directory.CreateDirectory(folder);
		var dbPath = Path.Combine(folder, "Rotovap.db");
		Console.WriteLine($"Database file should be at: {dbPath}");
		optionsBuilder.UseSqlite($"Data Source={dbPath}");

		return new RecipeDbContext(optionsBuilder.Options);
	}
}


